package com.njh.tools.base;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.apkfuns.logutils.LogUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * RecyclerView.Adapter父适配器
 * @param <T>
 */
public class RvBaseAdapter<T> extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    protected List<T> mBaseList = new ArrayList<>();
    public int viewtype;
    public Class<? extends BaseViewHolder> mItemViewClass;
    public Context mContext;

    public RvBaseAdapter(Context context, Class<? extends BaseViewHolder<T>> cla) {
        this.mContext = context;
        setView(cla);
    }

    /**
     * 设置传递的ViewHolder
     * @param cla
     */
    public void setView(Class<? extends BaseViewHolder<T>> cla) {
        try {
            BaseViewHolder mIVH = ((BaseViewHolder) (cla.getConstructor(View.class)
                    .newInstance(new View(mContext))));
            int mType = mIVH.getType();
            this.setViewType(mType, cla);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 初始化参数
     * @param i
     * @param cla
     */
    public void setViewType(int i, Class<? extends BaseViewHolder> cla) {
        this.viewtype = i;
        this.mBaseList = new ArrayList<>();
        this.mItemViewClass = cla;
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        return null!=mBaseList?mBaseList.size():0;
    }

    /**
     * 设置数据
     * @param datas
     */
    public void setBeans(List<T> datas) {
        if (null!=this.mBaseList){
            this.mBaseList.addAll(datas);
        }else{
            this.mBaseList=datas;
        }
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        try {
            return (RecyclerView.ViewHolder) (mItemViewClass)
                    .getConstructor(View.class).newInstance(
                            LayoutInflater.from(parent.getContext())
                                    .inflate(viewtype, parent,
                                            false));
        } catch (Exception e) {
            LogUtils.d("ViewHolderException", "onCreateViewHolder十有八九是xml写错了,哈哈");
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ((BaseViewHolder) holder).onBindViewHolder(holder.itemView, position);
    }

    /**
     *移除指定的一行
     * @param position
     */
    public void removeItem(int position) {
        mBaseList.remove(position);
        notifyItemRemoved(position);
    }

    /**
     * 更新指定的一行
     * @param position
     * @param item
     */
    public void upDateItem(int position, T item) {
        mBaseList.remove(position);
        mBaseList.add(position, item);
        notifyItemChanged(position);
    }
}